package comone.javatwo.Util;


import com.object.Order;
import comone.javatwo.Menus;
import comone.javatwo.User;

//通用
public class MenuBaseCurd<T> {

    //查询-->带条件，不带条件
    public void getBeseObject(T[] objects,T[] result) throws IllegalAccessException, InstantiationException {
         //如何遍历
        for (int i=0;i<result.length;i++){
            if( result[i] instanceof User){
                User user=(User)result[i];
                int id = user.getId();
                String name = user.getName();
                System.out.println("\t\t\t"+id+"\t\t\t"+name);
            }else  if(result[i] instanceof Menus){
                Menus menus=(Menus)result[i];
                int id = menus.getId();
                String name = menus.getmName();
            }
        }
    }

    //增，删，改
    public int baseUpdate(T[] objects,T[] result,int type){
          int count=0;
          if(type==1){
            for (int i=0;i<objects.length;i++){
                if(objects[i] instanceof  User){
                    int index = isNull(result);
                    result[index]=objects[i];
                    count++;
                }else if(objects[i] instanceof Order){

                }
            }
          }else if(type==2){
              for (int i=0;i<objects.length;i++){
                  Integer id = (Integer) objects[i];
                  int delete = delete(result, id);
                  count+=delete;
              }

          }else if(type==3){
          }
          return count;
    }

    //判断当前位置是否是空
    public int isNull(T[] result){
        int index=-1;
        for (int i=0;i<result.length;i++){
            if(result[i]==null){
                index=i;
                break;
            }
        }
        return index;
    }

    //根据条件删除对应位置的
    public int delete(T[] result,int id){
        int count=0;
        for (int i=0;i<result.length;i++){
              if(result[i]!=null && result[i] instanceof  User){
                  User user = (User)result[i];
                  if(user.getId()==id){
                      result[i]=null;
                      count++;
                      break;
                  }
              }else if(result[i]!=null && result[i] instanceof  Order){

            }
        }
        return count;
    }
}
